package service

import (
	"sync"

	"github.com/mars/internal/admin/core/domain"
)

// Service 持有所有 service 对象
type Service interface {
	UserSrv() UserSrv
	BookingSrv() BookingSrv
	ProductSrv() ProductSrv
	OrderSrv() OrderSrv
	CartSrv() CartSrv
}

type service struct {
	domain     domain.Factory
	userSrv    *userService
	bookingSrv *bookingService
	productSrv *productService
	orderSrv   *orderService
	cartSrv    *cartService
}

var _ Service = (*service)(nil)

var (
	_serviceOnce sync.Once
	se           *service
)

func NewService(domain domain.Factory) *service {
	if se != nil {
		return se
	}
	_serviceOnce.Do(func() {
		se = &service{
			domain:     domain,
			userSrv:    newUserService(domain),
			bookingSrv: newBookingService(domain),
			productSrv: newProductService(domain),
			orderSrv:   newOrderService(domain),
			cartSrv:    newCartService(domain),
		}
	})
	return se
}

func (s *service) UserSrv() UserSrv {
	return s.userSrv
}

func (s *service) BookingSrv() BookingSrv {
	return s.bookingSrv
}

func (s *service) ProductSrv() ProductSrv {
	return s.productSrv
}

func (s *service) OrderSrv() OrderSrv {
	return s.orderSrv
}

func (s *service) CartSrv() CartSrv {
	return s.cartSrv
}
